//
//  TestTCUBag.m
//  TreeMenuTest
//
//  Created by Etienne on 12/07/07.
//  Copyright (c) 2007 Etienne Samson. All rights reserved.
//

#import "TCUBagTests.h"

#import "TCUBag.h"
#import "TCUMutableBag.h"

@implementation TCUBagTests

- (void) testImmutable
{
    NSArray *array = [NSArray arrayWithObjects:@"object1", @"object1", @"object2", @"object1", @"object2", @"object3", nil];
    
    NSSet   *set = [NSSet setWithArray:array];
    TCUBag  *bag = nil;
    
    STAssertNoThrow ([TCUBag bag], @"");
    STAssertNoThrow ([[TCUBag alloc] init], @"");
    
    STAssertNoThrow (bag = [TCUBag bagWithArray:array], @"");
    STAssertEquals ([bag count], 6, @"");
    STAssertEquals ([bag countForObject:@"object1"], 3, @"");
    STAssertEquals ([bag countForObject:@"object2"], 2, @"");
    STAssertEquals ([bag countForObject:@"object3"], 1, @"");
    STAssertEquals ([bag countForObject:@"object4"], 0, @"");
    
    STAssertNoThrow (bag = [TCUBag bagWithSet:set], @"");
    
    STAssertEquals ([bag count], 3, @"");
    STAssertEquals ([bag countForObject:@"object1"], 1, @"");
    STAssertEquals ([bag countForObject:@"object2"], 1, @"");
    STAssertEquals ([bag countForObject:@"object3"], 1, @"");
    STAssertEquals ([bag countForObject:@"object4"], 0, @"");
    
    STAssertTrue ([bag containsObject:@"object3"], @"");
    STAssertFalse ([bag containsObject:@"object4"], @"");
    
    STAssertEquals ([bag member:@"object3"], @"object3", @"");
    STAssertEqualObjects ([bag member:@"object4"], nil, @"");
    
    NSArray *array2 = [NSArray arrayWithObjects:@"object1", @"object2", @"object3", nil];
    STAssertTrue ([bag isEqual:[TCUBag bagWithArray:array2]], @"");
    
    NSData* data;
    TCUBag* newBag;
    STAssertThrows (data = [NSArchiver archivedDataWithRootObject:bag], @"");
    STAssertNoThrow (data = [NSKeyedArchiver archivedDataWithRootObject:bag], @"");
    STAssertNoThrow (newBag = [NSKeyedUnarchiver unarchiveObjectWithData:data], @"");
    
    STAssertEqualObjects (bag, newBag, @"");
}

- (void) testMutable
{
    NSArray *array = [NSArray arrayWithObjects:@"object1", @"object1", @"object2", @"object1", @"object2", @"object3", nil];
    
    TCUMutableBag * bag = nil;
    
    STAssertNoThrow (bag = [[TCUMutableBag alloc] initWithArray:array], @"");
    
    STAssertNoThrow ([bag addObject:@"object3"], @"");
    STAssertThrows ([bag addObject:nil], @"");
    STAssertEquals ([bag countForObject:@"object3"], 2, @"");
    STAssertNoThrow ([bag addObject:@"object4"], @"");
    
    STAssertNoThrow ([bag removeObject:@"object1"], @"");
    STAssertThrows ([bag removeObject:nil], @"");
    
    STAssertNoThrow ([bag removeObject:@"object4"], @"");
    STAssertNoThrow ([bag removeObject:@"object3"], @"");
    STAssertNoThrow ([bag addObject:@"object1"], @"");
    
    STAssertEqualObjects (bag, [TCUBag bagWithArray:array], @"");
    STAssertTrue ([bag isEqual:[TCUBag bagWithArray:array]], @"");
    
    STAssertNoThrow ([bag removeAllObjects], @"");
    STAssertEquals ([bag count], 0, @"");
}
@end
